package com.mercator.client.graph;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class GeneSetMap<N extends Node<GenType,N>,U extends N,V extends N> extends Graph<GenType,N> {

	private HashSet<N> connected;
	private SortedSet<N> genes;
	private SortedSet<N> genesets;
	
	private int deg_thr = -2;
	
	public GeneSetMap() {
		super();
		connected = new HashSet<N>();
		genes = new TreeSet<N>(new GeneComparator<N>());
		genesets = new TreeSet<N>(new GeneComparator<N>());
	}
	
	
	public GeneSetMap(int deg_thr) {
		this();
		this.deg_thr = deg_thr;
	}
	public void connectGenes(U v1, V v2){
		super.connect(find(v1), find(v2));
		//System.out.println(degree(v1) + " : " + degree(v2));
			if(degree(find(v1)) > deg_thr)
				genes.add(find(v1));
			if(degree(find(v2)) > deg_thr)
				genesets.add(find(v2));		
	}
	
	@Override
	public Iterator<N> iterator(){
		return connected.iterator();
	}
	
	public Set<N> getGenes(){
		return genes;
	}
	
	public Set<N> getGeneSets(){
		return genesets;
	}
	
	public Set<GenType> getGeneSetsElems(){
		Set<GenType> s = new TreeSet<GenType>();
		for(N n : genesets)
			s.add(n.element);
		return s;
	}
	private class GeneComparator<E extends N> implements Comparator<E>{
		@Override
		public int compare(E node1, E node2) {
			if(node1.getDegree() > node2.getDegree())
				return -1;
			if(node1.getDegree() < node2.getDegree())
				return 1;
			return node1.compareTo(node2);
		}
		
	}

}
